<template>
  <el-container class="home-container">
    <el-header>
      <div @click="gohome()">
        <!--头部区域-->
        <img src="../assets/images/login_icon.png" alt="login_icon" />
        <span>微信小程序商家管理平台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!--页面主题区域-->
    <el-container>
      <!--侧边栏区域-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="#0f426b" text-color="#fff" active-text-color="#409eff" 
        unique-opened :collapse="isCollapse" :collapse-transition="false" :router="true"
        :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!--一级菜单模板区域-->
            <template slot="title">
              <!--图标-->
              <i :class="iconObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" 
            :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <!--二级菜单模板区域-->
            <template slot="title">
              <!--图标-->
              <i class="el-icon-menu"></i>
              <!--文本-->
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--内容区域-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      // 左侧菜单数据
      menulist:[],
      list: [],
      iconObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-zongzhixinxijiyewu',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-zongzhilingdaozerenzhi',
        '145':'iconfont icon-shuju',
      },
      // 是否折叠
      isCollapse:false,
      // 被激活的链接地址
      activePath:''
    }
  },
  // 创建组件时立即获取菜单
  created(){     
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear() /* 清除保存到客户端的 token */
      this.$router.push('/login') /* 重新回到 login 页面 */
    },
    // 返回首页
    gohome() {
      this.$router.push('/home')
    },
    // 获取所有菜单
    async getMenuList(){
      this.list=[]
      const {data:res}=await this.$http.get('menus')     /* 获得接口中的 menus */
      if(res.meta.status!==200) return this.$message.console.error(res.meta.msg);
      this.menulist=res.data
      var storage=window.localStorage;
      res.data.forEach(item => {
        this.list.push(item.authName);
        item.children.forEach(item1 => {
          this.list.push(item1.authName)
        });
      });
      storage["menulist"]=this.list
    },
    // 切换菜单的折叠与展开
    toggleCollapse(){
      this.isCollapse=!this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #0f426b;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  > div {
    display: flex;
    align-items: center;
    height: 30px;
    span {
      margin-left: 15px;
    }
    img {
      height: 100%;
      background-repeat: no-repeat;
      background-position: left center;
      margin: 0 2px 0 7px;
    }
  }
}
.el-aside {
  background-color: #0f426b;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 15px;
}
.toggle-button{
  background-color: #103e64;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
